package org.lgiavedoni.android.franq.util.velocity;

import java.io.File;
import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.lgiavedoni.android.franq.service.log.Logger;

public class VelocityUtil {

	private static VelocityEngine ve;
	private static Template template;

	static {
		ve = new VelocityEngine();
		try {
			// Disable the velocity logs. In other case the system throw a
			// exception for the log file
			ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
			ve.init();
		} catch (Exception e) {
			Logger.inst.error(e);
		}
	}

	public static String run(File templateFile, VelocityContext context) {
		try {
			template = ve.getTemplate(templateFile.getPath());
			/* create a context and add data */
			StringWriter writer = new StringWriter();
			template.merge(context, writer);
			return writer.toString();
		} catch (Exception e) {
			Logger.inst.error(e);
			return e.toString();
		}
	}

}
